﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Qetza.TeamFoundation.WorkItemQuery.Admin
{
    /// <summary>
    /// Command to rename and/or move a work item query.
    /// </summary>
    public class CommandRenameWiq : ICommand
    {
        #region ICommand Members

        /// <inheritdoc />
        public string Name
        {
            get { return "rename"; }
        }

        /// <inheritdoc />
        public string HelpText
        {
            get { return ResourceStrings.RenameWiqHelpText; }
        }

        /// <inheritdoc />
        public IEnumerable<ParameterInfo> AllowedParameters
        {
            get
            {
                return new[] {
                    new ParameterInfo() { 
                        Name = CoreArgumentNames.Collection, 
                        ArgumentName = ResourceStrings.ProjectCollectionArgument, 
                        IsRequired = true, 
                        HelpText = ResourceStrings.ProjectCollectionHelpText
                        },
                    new ParameterInfo() { 
                        Name = CoreArgumentNames.Project, 
                        ArgumentName = ResourceStrings.ProjectArgument, 
                        IsRequired = true, 
                        HelpText = ResourceStrings.ProjectHelpText
                        }, 
                    new ParameterInfo() {
                        Name = CoreArgumentNames.QueryPath,
                        ArgumentName = ResourceStrings.QueryPathArgument,
                        IsRequired = true,
                        HelpText = ResourceStrings.Rename_QueryPathHelpText
                        }, 
                    new ParameterInfo() {
                        Name = CoreArgumentNames.New,
                        ArgumentName = ResourceStrings.QueryNewPathArgument,
                        IsRequired = true,
                        HelpText = ResourceStrings.Rename_QueryNewPathHelpText
                        }, 
                    new ParameterInfo() {
                        Name = CoreArgumentNames.NoPrompt,
                        IsFlag = true,
                        HelpText = ResourceStrings.NoPromptHelpText
                        }
                    };
            }
        }

        /// <inheritdoc />
        public void Execute(IDictionary<string, string> parameters)
        {
            if (parameters.ContainsKey(CoreArgumentNames.NoPrompt) || 
                Utility.Confirm(string.Format(ResourceStrings.RenameWiqConfirm, parameters[CoreArgumentNames.QueryPath], parameters[CoreArgumentNames.New])))
            {
                // get query
                var queries = TfsUtility
                    .GetWorkItemStore(parameters[CoreArgumentNames.Collection], parameters.ContainsKey(CoreArgumentNames.NoPrompt))
                    .Projects[parameters[CoreArgumentNames.Project]]
                    .QueryHierarchy;
                var query = queries.Find(parameters[CoreArgumentNames.QueryPath]);

                if (query != null)
                {
                    // rename and/or move query
                    var index = parameters[CoreArgumentNames.QueryPath].LastIndexOf('/');
                    var folderPath = (index == -1 ? string.Empty : parameters[CoreArgumentNames.QueryPath].Substring(0, index));
                    var queryName = parameters[CoreArgumentNames.QueryPath].Substring(index + 1);

                    index = parameters[CoreArgumentNames.New].LastIndexOf('/');
                    var newFolderPath = (index == -1 ? string.Empty : parameters[CoreArgumentNames.New].Substring(0, index));
                    var newQueryName = parameters[CoreArgumentNames.New].Substring(index + 1);

                    // rename and/or move query
                    var newFolder = queries.CreateFolder(newFolderPath);
                    query.Name = newQueryName;

                    newFolder.Add(query);

                    queries.Save();
                    Console.WriteLine(ResourceStrings.RenameWiqCompleted);
                }
            }
        }

        /// <inheritdoc />
        public void ValidateParameters(IDictionary<string, string> parameters)
        {
            return;
        }

        #endregion
    }
}
